<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <BasicDrawer
    v-bind="$attrs"
    :showFooter="true"
    :okAuth="'mo:route:mesOrderRoute:edit'"
    @register="registerDrawer"
    @ok="handleSubmit"
    width="60%"
  >
    <template #title>
      <Icon :icon="getTitle.icon" class="pr-1 m-1" />
      <span> {{ getTitle.value }} </span>
    </template>
    <BasicForm @register="registerForm" />
  </BasicDrawer>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsMoRouteMesOrderRouteForm',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, ref, computed } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicForm, FormSchema, useForm } from '/@/components/Form';
  import { BasicDrawer, useDrawerInner } from '/@/components/Drawer';
  import {
    MesOrderRoute,
    mesOrderRouteSave,
    mesOrderRouteForm,
  } from '/@/api/mes/mo/route/mesOrderRoute';

  const emit = defineEmits(['success', 'register']);

  const { t } = useI18n('mo.route.mesOrderRoute');
  const { showMessage } = useMessage();
  const record = ref<MesOrderRoute>({} as MesOrderRoute);
  const getTitle = computed(() => ({
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: record.value.isNewRecord ? t('新增订单工艺路线') : t('编辑订单工艺路线'),
  }));

  const inputFormSchemas: FormSchema[] = [
    {
      label: t('订单号'),
      field: 'scOrder.code',
      component: 'Input',
      componentProps: {
        maxlength: 64,
        disabled: true,
      },
    },
    {
      label: t('行号'),
      field: 'scOrder.sortNum',
      component: 'Input',
      componentProps: {
        maxlength: 64,
        disabled: true,
      },
    },
    {
      label: t('工序编码'),
      field: 'operCode',
      component: 'Input',
      componentProps: {
        maxlength: 64,
        disabled: true,
      },
    },
    {
      label: t('加工顺序'),
      field: 'sortNum',
      component: 'Input',
      componentProps: {
        maxlength: 8,
      },
      rules: [{ pattern: /^\d+$/, message: t('请输入一个正整数') }],
    },
    {
      label: t('关键工序'),
      field: 'bkey',
      component: 'CheckboxGroup',
      componentProps: {
        options: [{ label: '', value: '1' }],
      },
    },
    {
      label: t('计价方式'),
      field: 'costType',
      component: 'Select',
      componentProps: {
        dictType: 'mes_price_mode',
        allowClear: true,
      },
    },
    {
      label: t('是否委外'),
      field: 'bout',
      component: 'CheckboxGroup',
      componentProps: {
        options: [{ label: '', value: '1' }],
      },
    },
    {
      label: t('是否报工'),
      field: 'breport',
      component: 'CheckboxGroup',
      componentProps: {
        options: [{ label: '', value: '1' }],
      },
    },
    {
      label: t('是否检验'),
      field: 'bcheck',
      component: 'CheckboxGroup',
      componentProps: {
        // dictType: 'sys_yes_no',
        options: [{ label: '', value: '1' }],
      },
    },
    {
      label: t('是否上机'),
      field: 'bmachine',
      component: 'CheckboxGroup',
      componentProps: {
        // dictType: 'sys_yes_no',
        options: [{ label: '', value: '1' }],
      },
    },
    // {
    //   label: t('是否检验'),
    //   field: 'bcheck',
    //   component: 'Checkbox',
    //   componentProps: ({ formModel }) => {
    //     return {
    //       options: [{ label: '', value: 1 }],
    //       onChange: (v) => {
    //         console.log(v.target);
    //         formModel['bcheck'] = v.target.checked ? 1 : 0;
    //       },
    //     };
    //   },
    //   //required: true,
    // },
    {
      label: t('技能标准'),
      field: 'techLevel',
      component: 'CheckboxGroup',
      componentProps: {
        dictType: 'mes_tech_level',
      },
    },
    {
      label: t('技能控制'),
      field: 'techCtrl',
      component: 'Select',
      componentProps: {
        dictType: 'mes_tech_ctrl',
        allowClear: true,
      },
    },
    {
      label: t('报工方式'),
      field: 'reportType',
      component: 'Select',
      componentProps: {
        dictType: 'mes_report_type',
        allowClear: true,
      },
    },
    {
      label: t('计件单价'),
      field: 'iprice',
      component: 'Input',
      componentProps: {
        maxlength: 16,
      },
      rules: [
        { pattern: /^(?:-?\d+|-?\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/, message: t('请输入一个数值') },
      ],
    },
    {
      label: t('工序说明'),
      field: 'remarks',
      component: 'InputTextArea',
      componentProps: {
        maxlength: 500,
      },
      colProps: { lg: 24, md: 24 },
    },
  ];

  const [registerForm, { resetFields, setFieldsValue, validate }] = useForm({
    labelWidth: 120,
    schemas: inputFormSchemas,
    baseColProps: { lg: 24, md: 24 },
  });

  const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(async (data) => {
    setDrawerProps({ loading: true });
    await resetFields();
    const res = await mesOrderRouteForm(data);
    record.value = (res.mesOrderRoute || {}) as MesOrderRoute;
    record.value.__t = new Date().getTime();
    setFieldsValue(record.value);
    //  record.value.scOrder.orderStatus 为4 或者 9 则不显示确认按钮
    setDrawerProps({ loading: false , showOkBtn: record.value.scOrder.orderStatus!='4' && record.value.scOrder.orderStatus!='9' });
  });

  async function handleSubmit() {
    try {
      const data = await validate();
      setDrawerProps({ confirmLoading: true });
      const params: any = {
        isNewRecord: record.value.isNewRecord,
        id: record.value.id,
      };
      // console.log('submit', params, data, record);
      const res = await mesOrderRouteSave(params, data);
      showMessage(res.message);
      setTimeout(closeDrawer);
      emit('success', data);
    } catch (error: any) {
      if (error && error.errorFields) {
        showMessage(t('您填写的信息有误，请根据提示修正。'));
      }
      console.log('error', error);
    } finally {
      setDrawerProps({ confirmLoading: false });
    }
  }
</script>
